﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using System.Data;
using MySql.Data.MySqlClient;
using Common;

namespace DAL
{
    /// <summary>
    /// 包装数据处理类
    /// </summary>
    public class PackageDAL
    {
        //转化为模型
        private Packages TOPackages(DataRow dr) {
            Packages p = new Packages();
            p.Pid = Convert.ToInt32(dr["Pid"]);
            p.Ptype = dr["Ptype"].ToString();
            p.Price = dr["Price"].ToString();
            p.AddUser = dr["AddUser"].ToString();
            p.EditTime = dr["EditTime"].ToString();
            p.AddTime = dr["AddTime"].ToString();
            p.EditUser = dr["EditUser"].ToString();
            return p;
        }
        //查询所有
        public DataTable GetpackageListAll(string ptype) {
            string sql = string.Empty;
            if (!string.IsNullOrWhiteSpace(ptype)) {
                sql = " and ptype like '%" + ptype + "%'";
            }
            return SqlHelper.ExecuteDataTable("select * from package where isdel=@isdel "+sql,new MySqlParameter("@isdel","0"));

        }
        //删除类别
        public bool DelById(string pid) {
            int result = SqlHelper.ExecuteNonQuery("update package set isdel=1 where pid =@pid",new MySqlParameter("@pid",pid));
            if (result == 1)
            {
                return true;
            }
            else {
                return false;
            }
        }
        //根据id查询包装明细
        public Packages GetPackagesById(string pid) {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from package where isdel=0 and pid=@pid",new MySqlParameter("@pid",pid));
            if (dt.Rows.Count > 0)
            {
                return TOPackages(dt.Rows[0]);
            }
            else {
                return null;
            }
        }

        //保存数据
        public int SaveData(Packages p) {
            return SqlHelper.ExecuteNonQuery("insert into package(ptype,price,addtime,adduser,edittime,edituser,isdel) values(@ptype,@price,@addtime,@adduser,@edittime,@edituser,@isdel)",
                new MySqlParameter("@ptype", p.Ptype), new MySqlParameter("@price", p.Price), new MySqlParameter("@addtime", DateTime.Now.ToString()), new MySqlParameter("@adduser", RemeberName.Username),
                new MySqlParameter("@edittime", DateTime.Now.ToString()), new MySqlParameter("@edituser", RemeberName.Username),new MySqlParameter("@isdel","0"));
        }
        //更新数据
        public int UpdateData(Packages p) {
            return SqlHelper.ExecuteNonQuery("update package set ptype=@ptype,price=@price,edituser=@edituser,edittime=@edittime where pid=@pid and isdel =0",
                new MySqlParameter("@ptype", p.Ptype), new MySqlParameter("@price", p.Price), new MySqlParameter("@edituser", RemeberName.Username), new MySqlParameter("@edittime", DateTime.Now.ToString()), new MySqlParameter("@pid", p.Pid));
        }
    }
}
